package com.ruoyi.wm.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.wm.domain.WmApparatus;
import com.ruoyi.wm.domain.WmApparatusInventory;
import com.ruoyi.wm.mapper.WmApparatusInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wm.mapper.WmApparatusInstorageListMapper;
import com.ruoyi.wm.domain.WmApparatusInstorageList;
import com.ruoyi.wm.service.IWmApparatusInstorageListService;
import com.ruoyi.common.core.text.Convert;

/**
 * 医疗器械入库记录Service业务层处理
 *
 * @author hpf
 * @date 2021-11-25
 */
@Service
public class WmApparatusInstorageListServiceImpl implements IWmApparatusInstorageListService {
    @Autowired
    private WmApparatusInstorageListMapper wmApparatusInstorageListMapper;
    @Autowired
    private WmApparatusInventoryMapper wmApparatusInventoryMapper;

    /**
     * 查询医疗器械入库记录
     *
     * @param id 医疗器械入库记录ID
     * @return 医疗器械入库记录
     */
    @Override
    public WmApparatusInstorageList selectWmApparatusInstorageListById(Long id) {
        return wmApparatusInstorageListMapper.selectWmApparatusInstorageListById(id);
    }

    /**
     * 查询医疗器械入库记录列表
     *
     * @param wmApparatusInstorageList 医疗器械入库记录
     * @return 医疗器械入库记录
     */
    @Override
    public List<WmApparatusInstorageList> selectWmApparatusInstorageListList(WmApparatusInstorageList wmApparatusInstorageList) {
        return wmApparatusInstorageListMapper.selectWmApparatusInstorageListList(wmApparatusInstorageList);
    }

    /**
     * 新增医疗器械入库记录
     *
     * @param wmApparatusInstorageList 医疗器械入库记录
     * @return 结果
     */
    @Override
    public int insertWmApparatusInstorageList(WmApparatusInstorageList wmApparatusInstorageList) {
        //TODO
        addIntoInventory(wmApparatusInstorageList);
        return wmApparatusInstorageListMapper.insertWmApparatusInstorageList(wmApparatusInstorageList);
    }

    /**
     * 修改医疗器械入库记录
     *
     * @param wmApparatusInstorageList 医疗器械入库记录
     * @return 结果
     */
    @Override
    public int updateWmApparatusInstorageList(WmApparatusInstorageList wmApparatusInstorageList) {
        return wmApparatusInstorageListMapper.updateWmApparatusInstorageList(wmApparatusInstorageList);
    }

    /**
     * 删除医疗器械入库记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmApparatusInstorageListByIds(String ids) {
        return wmApparatusInstorageListMapper.deleteWmApparatusInstorageListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除医疗器械入库记录信息
     *
     * @param id 医疗器械入库记录ID
     * @return 结果
     */
    @Override
    public int deleteWmApparatusInstorageListById(Long id) {
        return wmApparatusInstorageListMapper.deleteWmApparatusInstorageListById(id);
    }

    /**
     * 添加根据入库记录更新库存功能sercvice
     *
     * @date 2021-12-1
     */
    @Override
    public int updateApparatusStock() {
        return wmApparatusInstorageListMapper.updateStock();
    }

    public void addIntoInventory(WmApparatusInstorageList wmApparatusInstorageList) {
        String name = wmApparatusInstorageList.getName();
        WmApparatusInventory latestInventory = wmApparatusInventoryMapper.selectLatestIndex(name);

        WmApparatusInventory inventory = new WmApparatusInventory();
        inventory.setName(name);//设置名称
        inventory.setForm(wmApparatusInstorageList.getStandard());//设置规格
        inventory.setUnit(wmApparatusInstorageList.getUnit());//设置单位
        inventory.setTime(wmApparatusInstorageList.getTime());//设置时间
        Integer number = wmApparatusInstorageList.getNumber();
        inventory.setInStorageNum(new Long(number));//设置入库数量

        BigDecimal price = wmApparatusInstorageList.getPrice();

        BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(number));//计算入库总价

        inventory.setInPrice(price);
        inventory.setInTotalPrice(totalPrice);
        long remainNum;
        BigDecimal remainPrice;
        if (latestInventory!=null){//如果有上条记录
            long num = latestInventory.getRemainNum();//获取记录的剩余数量
            remainNum=num + wmApparatusInstorageList.getNumber();//剩余数量等于本次加上次
            remainPrice = latestInventory.getRemainTotalPrice().add(totalPrice);



        }
        else {
            inventory.setRemainNum(new Long(number));
            remainNum= wmApparatusInstorageList.getNumber();//剩余数量就是本次入库数量
            remainPrice = totalPrice;
        }
        inventory.setRemainTotalPrice(remainPrice);//计算剩余总价
        inventory.setRemainNum(remainNum);
        inventory.setRemianPrice(price);//设置剩余价格
        wmApparatusInventoryMapper.insertWmApparatusInventory(inventory);
    }
}
